﻿using System;
using System.Windows;

using CommStudio.Connections;
using MIC_LOGIC;

namespace MIC_GUI
{
    /// <summary>
    /// Popup with div. settings
    /// </summary>
	public partial class SettingsPopup
    {
        #region fields
        private SerialOptions _serialConfiguration;
        private LogStreamConfiguration _logconfiguration;
        #endregion

        #region properties
        /// <summary>
        /// Gets Or Sets whether OK button is pressed
        /// </summary>
        public bool IsOKPressed { get; set; }
        
        /// <summary>
        /// Gets or Sets Log stream configuration
        /// </summary>
        public LogStreamConfiguration LogConfiguration 
        {
            get
            {
                _logconfiguration.Log_channel_0 = chbAccel.IsChecked.Value;
                _logconfiguration.Log_channel_1 = chbGyro.IsChecked.Value;
                _logconfiguration.Log_channel_2 = chbAngle.IsChecked.Value;                
                return _logconfiguration;
            }
            set
            {
                chbAccel.IsChecked = value.Log_channel_0;
                chbGyro.IsChecked = value.Log_channel_1;
                chbAngle.IsChecked = value.Log_channel_2;
                _logconfiguration = value;
            }
        }

        /// <summary>
        /// Gets or Sets Serial Communication configuration
        /// </summary>
        public SerialOptions SerialCofiguration 
        {
            get 
            {
                _serialConfiguration.PortName = cbPortName.SelectedItem.ToString();
                try
                {
                    _serialConfiguration.BaudRate = Int32.Parse(tbBaudRate.Text);
                }
                catch { }

                _serialConfiguration.Parity = (Parity)cbParity.SelectedItem;
                try
                {
                    _serialConfiguration.DataBits = Int32.Parse(cbDataBits.SelectedItem.ToString());
                }
                catch { }

                _serialConfiguration.StopBits = (CommStopBits)cbStopBits.SelectedItem;

                _serialConfiguration.FlowControlDsrDtr = false;
                _serialConfiguration.FlowControlXonXoff = false;
                _serialConfiguration.FlowControlCtsRts = true;
                _serialConfiguration.FlowControlRs485 = false;
                _serialConfiguration.EnableDtr = true;
                _serialConfiguration.EnableRts = true;

                return _serialConfiguration; 
            }
            set
            {
                _serialConfiguration = value;

                cbPortName.SelectedItem = _serialConfiguration.PortName;
                tbBaudRate.Text = _serialConfiguration.BaudRate.ToString();
                cbParity.SelectedItem = _serialConfiguration.Parity;
                cbDataBits.SelectedItem = _serialConfiguration.DataBits;
                cbStopBits.SelectedItem = _serialConfiguration.StopBits;

            }
        }

        #endregion

        /// <summary>
        /// Constructor
        /// </summary>
        public SettingsPopup()
		{
			this.InitializeComponent();
            InitSerialSettings();
			// Insert code required on object creation below this point.
		}

        /// <summary>
        /// Inits serial settings with serial port names
        /// </summary>
        void InitSerialSettings()
        {
            foreach (SerialPortInfo item in SerialPortInfo.Ports)
            {
                cbPortName.Items.Add(item.Name);
            }

            tbBaudRate.Text = "" + 0;
            
            cbParity.Items.Add(Parity.Even);
            cbParity.Items.Add(Parity.Mark);
            cbParity.Items.Add(Parity.None);
            cbParity.Items.Add(Parity.Odd);
            cbParity.Items.Add(Parity.Space);

            cbDataBits.Items.Add(5);
            cbDataBits.Items.Add(6);
            cbDataBits.Items.Add(7);
            cbDataBits.Items.Add(8);

            cbStopBits.Items.Add(CommStopBits.One);
            cbStopBits.Items.Add(CommStopBits.OnePointFive);
            cbStopBits.Items.Add(CommStopBits.Two);
        }


        #region event handlers
        void OK_Click(object sender, RoutedEventArgs e)
        {
            IsOKPressed = true;
            this.Close();
        }

        void Cancel_Click(object sender, RoutedEventArgs e)
        {
            IsOKPressed = false;
            this.Close();
        }
        #endregion
    }
}